package lcr;

import java.util.HashMap;
import java.util.Map;

public class Test0011 {
    public static void main(String[] args)
    {
        int[] nums = {0,1,0};
        Solution0011_1 solution0011_1 = new Solution0011_1();
        System.out.println(solution0011_1.findMaxLength(nums));
    }
}
class Solution0011_1 {
    public int findMaxLength(int[] nums) {

        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 0) nums[i] = -1;
        }

        int sum = 0, maxLen = 0;
        Map<Integer, Integer> map = new HashMap<>();
        map.put(0, -1);

        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
            if (map.containsKey(sum)) {
                maxLen = Math.max(maxLen, i - map.get(sum));
            } else {
                map.put(sum, i);
            }
        }

        return maxLen;
    }
}
